<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('menu.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('menu.title')" prop="title">
            <el-input :placeholder="i18n('common.please_input') + i18n('menu.title')" v-model="dataForm.title"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('menu.content')" prop="content">
            <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('menu.content')" v-model="dataForm.content"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('menu.parent_title')" prop="parent_id">
            <el-cascader
              v-model="dataForm.parent_id"
              :placeholder="i18n('common.please_select') + i18n('menu.parent_title')"
              :show-all-levels="false"
              :options="levellist"
              :props="levelProps"
              clearable></el-cascader>
          </el-form-item>

          <el-form-item :label="i18n('menu.category_info')" prop="category_id">
            <el-select
              v-model="dataForm.category_id"
              :placeholder="i18n('common.please_input') + i18n('menu.category_info')">
              <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('menu.type')" prop="type">
            <el-select v-model="dataForm.type" :placeholder="i18n('common.please_input') + i18n('menu.type')">
              <el-option v-for="(v, k) in typeList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('menu.icon')" prop="icon">
            <el-input :placeholder="i18n('common.please_input') + i18n('menu.icon')" v-model="dataForm.icon"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('menu.url')" prop="url">
            <el-input :placeholder="i18n('common.please_input') + i18n('menu.url')" v-model="dataForm.url"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('menu.is_visible')" prop="is_visible">
            <el-select v-model="dataForm.is_visible" :placeholder="i18n('common.please_input') + i18n('menu.is_visible')">
              <el-option v-for="(v, k) in visibleList" :label="v.title" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('menu.is_hidden')" prop="is_hidden">
            <el-switch
              v-model="dataForm.is_hidden"
              active-value="1"
              :active-text="i18n('common.show')"
              inactive-value="2"
              :inactive-text="i18n('common.hidden')">
            </el-switch>
          </el-form-item>

          <el-form-item :label="i18n('common.sort')" prop="sort">
            <el-input-number :placeholder="i18n('common.please_select') + i18n('common.sort')" v-model="dataForm.sort"></el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('kernel:menu:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="menu-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useStore } from "@/hooks/store/useStore"

  const { user } = useStore()
  const { router, query, i18n, isAuth } = useBase()

  const levellist = ref([])
  const model = ref('menu')
  const categoryList = ref([])

  const typeList = reactive([
    { id: 1, title: '菜单' },
    { id: 2, title: '按钮' },
    { id: 3, title: '菜单|按钮' },
  ])

  const visibleList = reactive([
    { id: 1, title: '全体可见' },
    { id: 2, title: '平台可见' },
    { id: 3, title: '机构可见' },
  ])

  const levelProps = reactive({
    label: 'title',
    value: 'id',
    checkStrictly: true,
  });

  const dataForm = reactive({
    id: 0,
    title: '',
    parent_id: 0,
    content: '',
    category_id: '',
    type: 1,
    icon: '',
    url: '',
    is_visible: 1,
    is_hidden: '2',
    sort: 0,
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('menu.rules.title.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/menu/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.title = data.data.title
            dataForm.parent_id = data.data.parent_id
            dataForm.content = data.data.content
            dataForm.category_id = data.data.category_id
            dataForm.type = parseInt(data.data.type.value)
            dataForm.icon = data.data.icon
            dataForm.url = data.data.url
            dataForm.is_visible = data.data.is_visible
            dataForm.is_hidden = data.data.is_hidden + ''
            dataForm.sort = data.data.sort
          }
        })
      }
    })
  }

  // 获取菜单级别数据列表
  const loadLevelList = () => {
    $http({
      url: $http.adornUrl('/menu/level'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        levellist.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  // 获取菜单分类数据列表
  const getCategoryList = () => {
    $http({
      url: $http.adornUrl('/menu/category/select'),
      method: 'get',
      params: $http.adornParams(),
    }).then(({ data }) => {
      categoryList.value = data.data
    })
  }

  onMounted(() => {
    init(formRef)
    loadLevelList()
    getCategoryList()
  })
</script>
